#/bin/bash

LROOT=$PWD
JOBCOUNT=${JOBCOUNT=$(nproc)}
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-

export KERNEL_PATH=$1
echo "kernel path $KERNEL_PATH"

export NAME_FLAG=$2
echo "name flag $NAME_FLAG"

echo "cp Image to /home/coresat/tftproot/Image_${KERNEL_PATH}_$NAME_FLAG"


export BUILD_OUTPUT=$LROOT/${KERNEL_PATH}_${NAME_FLAG}_build_output

echo "output path $BUILD_OUTPUT"

cd $KERNEL_PATH

#
#case 1 - normal build
#

# {
#     make mrproper
#     make defconfig
#     make modules_prepare
#     make -j$(nproc)
# }

#
#case 2 - build output to a folder
#

echo "make distclean"
make distclean


echo "build output: " $BUILD_OUTPUT

rm -rf $BUILD_OUTPUT
mkdir -p $BUILD_OUTPUT

echo "make defconfig"
make defconfig O=$BUILD_OUTPUT

echo "make menuconfig"
make menuconfig O=$BUILD_OUTPUT

make O=$BUILD_OUTPUT -j $JOBCOUNT

echo "cp Image to /home/coresat/tftproot/Image_${KERNEL_PATH}_$NAME_FLAG"
sudo cp $BUILD_OUTPUT/arch/arm64/boot/Image /home/coresat/tftproot/Image_${KERNEL_PATH}_$NAME_FLAG


